package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.icss.beans.StuClassVO;
import com.icss.beans.User;
import com.icss.daos.Dao;
import com.icss.daos.FileCenterDao;
import com.icss.daos.UserDao;

public class UserManage extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserManage() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		User u = (User) session.getAttribute("User");
		UserDao ud = new UserDao();

		String action = request.getParameter("action");
		if (action.equals("getUserInfo")) {
			User user = ud.getUserInfo(u.getUid());
			request.setAttribute("UserInfo", user);
			ArrayList<StuClassVO> scList = ud.getStuClass();

			request.setAttribute("stuClassList", scList);

			request.getRequestDispatcher("/page/userManage/userInfo.jsp").forward(request, response);
		} else if (action.equals("udpass")) {
			User user = ud.getUserInfo(u.getUid());
			session.setAttribute("UserInfo", user);

			request.getRequestDispatcher("/page/userManage/udPassword.jsp").forward(request, response);
		} else if (action.equals("editpasswword")) {
			String password = request.getParameter("PASS1");
			if (ud.updatePassWord(u.getUid(), password)) {
				request.setAttribute("OK", "udpass");
				request.getRequestDispatcher("/page/userManage/updateok.jsp").forward(request, response);
			}

		} else if (action.equals("updateuserinfo")) {
			int year = Integer.parseInt(request.getParameter("BIR_YEAR"));

			int month = Integer.parseInt(request.getParameter("BIR_MON"));
			int day = Integer.parseInt(request.getParameter("BIR_DAY"));

			Calendar cal = Calendar.getInstance();
			cal.set(year, month - 1, day);
			Date birthday = new Date(cal.getTime().getTime());
			Date eduTime = new Date(2010, 6, 15);
			User ui = new User();
			System.out.println("---------------------------------------" + birthday.toString());
			ui.setAddress(request.getParameter("address"));
			ui.setBirthday(birthday);
			ui.setUid(u.getUid());
			ui.setEduSchool(request.getParameter("eduSchool"));
			ui.setEduTime(eduTime);
			ui.setEmail(request.getParameter("email"));
			ui.setMsn(request.getParameter("MSN"));
			ui.setPhone(request.getParameter("phone"));
			ui.setQq(request.getParameter("QQ"));
			ui.setRemark(request.getParameter("remark"));
			ui.setSchoolType(request.getParameter("schoolType"));
			ui.setSex(request.getParameter("sex"));
			ui.setPost(request.getParameter("post"));
			ui.setHomeTel(request.getParameter("homeTel"));
			ui.setStuClassId(Integer.parseInt(request.getParameter("stuCalssId")));

			if (ud.updateuserInfo(ui, 0)) {
				request.setAttribute("OK", "udui");
				request.getRequestDispatcher("/page/userManage/updateok.jsp").forward(request, response);
			} else {
				System.out.println("修改失败！！！");
			}

		} else if ("userList".equals(action)) {
			ArrayList<User> list = ud.getAllUser();
			
			request.setAttribute("userList", list);
			request.getRequestDispatcher("/page/userManage/showUser.jsp").forward(request, response);

		} else if ("editUser".equals(action)) {
			int uid = Integer.parseInt(request.getParameter("uid"));
			User user = ud.getUserInfo(uid);
			ArrayList<StuClassVO> scList = ud.getStuClass();

			request.setAttribute("stuClassList", scList);
			request.setAttribute("editUser", user);
			request.getRequestDispatcher("/page/userManage/editUser.jsp").forward(request, response);

		} else if ("updateUser".equals(action)) {
			int uid = Integer.parseInt(request.getParameter("uid"));
			String userName = request.getParameter("userName");
			int stuclassId = Integer.parseInt(request.getParameter("stuClassId"));
			String userPassword = request.getParameter("userPassword");
			User user = new User();
			user.setUserName(userName);
			user.setUserPassword(userPassword);
			user.setStuClassId(stuclassId);
			user.setUid(uid);
			if (ud.updateuserInfo(user, 1)) {
				request.setAttribute("OK", "updateUser");
				request.getRequestDispatcher("/page/userManage/updateok.jsp").forward(request, response);
			}
			//request.setAttribute("editUser", user);
		} else if ("deleteUser".equals(action)) {
			int uid = Integer.parseInt(request.getParameter("uid"));

			if (ud.deleteUser(uid)) {
				ArrayList<User> list = ud.getAllUser();

				request.setAttribute("userList", list);
				request.getRequestDispatcher("/page/userManage/showUser.jsp").forward(request, response);
			}
		} else if ("deleteAllUser".equals(action)) {
			String uidlist = request.getParameter("uidList");

			if (ud.deleteAllUser(uidlist)) {
				ArrayList<User> list = ud.getAllUser();

				request.setAttribute("userList", list);
				request.getRequestDispatcher("/page/userManage/showUser.jsp").forward(request, response);
			}
		} else if ("addUser".equals(action)) {
			String[] userNameList = request.getParameterValues("userName");
			String[] userpwList = request.getParameterValues("userPassword");
			String[] stuClassId = request.getParameterValues("stuClassId");
			
			String webTempPath = getServletContext().getRealPath("/") + "upload";
			
			FileCenterDao fileDao = new FileCenterDao();
			String fURL = fileDao.ChangeURL(webTempPath);
				
			ud.addUsers(userNameList, userpwList,stuClassId,fURL);
			request.getRequestDispatcher("/page/userManage/addUserOK.jsp").forward(request, response);
		} else if ("findclass".equals(action)) {
			ArrayList<StuClassVO> scList = ud.getStuClass();
			JSONArray json = JSONArray.fromObject(scList);
			out.print(json);

		} else if ("toBeAdmin".equals(action)) {
			String uid = request.getParameter("uid");
			String sql = "update users set pid=4 where uid=" + uid;
			Dao dao = new Dao();
			response.setContentType("text/html");
			PrintWriter out2 = response.getWriter();
			String url = "";
			if (dao.execute(sql)) {
				url = "<script>alert('设置成功');window.location.href='UserManage?action=userList';</script>";
			} else {
				url = "<script>alert('设置失败');window.location.href='UserManage?action=userList';</script>";
				
			}
			
			out2.print(url);
			out2.close();
			dao.close();
		}else if ("cancleAdmin".equals(action)) {
			
			String uid = request.getParameter("uid");
			String sql = "update users set pid=3 where uid=" + uid;
			Dao dao = new Dao();
			response.setContentType("text/html");
			PrintWriter out2 = response.getWriter();
			String url = "";
			if (dao.execute(sql)) {
				url = "<script>alert('设置成功');window.location.href='UserManage?action=userList';</script>";
			} else {
				url = "<script>alert('设置失败');window.location.href='UserManage?action=userList';</script>";
				
			}
			
			out2.print(url);
			out2.close();
			dao.close();
		}
		
		ud.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
